<?php
namespace app\admin\service;

use app\admin\model\yun\YunPhoneUserMachine;
use app\qsapi\service\YunPhoneService;
/**
 * 云挂机服务类
 */
class BoxYunUserMachineService extends BaseService
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new YunPhoneUserMachine();
    }


    public function getList(): array
    {
        $params = request()->param();
        $where = [];

        if (!empty($params['time_range'][0]) && !empty($params['time_range'][1])) {
            $where[] = ['create_time', 'between', [$params['time_range'][0], $params['time_range'][1]]];
        }
        if (!empty($params['running_status'])) {
            $where[] = ['displayStatus', '=', $params['running_status']];
        }
        if (!empty($params['member_id'])) {
            $where[] = ['member_id', '=', $params['member_id']];
        }
        if (!empty($params['user_phone_id'])) {
            $where[] = ['user_device_id', '=', $params['user_phone_id']];
        }
        $where[] = ['valid_end_time', '>', date("Y-m-d H:i:s")];
        $data = $this->model->where($where)->order('id', 'desc')->page(PAGE, PERPAGE)->select();
        foreach ($data as &$item) {
            $item['running_status_name'] = YunPhoneUserMachine::displayStatus[$item['displayStatus']] ?? '离线';
            $item['onHook_status_name'] = YunPhoneUserMachine::onHookState[$item['onHookState']] ?? '未挂机';
            $item['status_name'] = YunPhoneUserMachine::phoneStatus[$item['status']] ?? '未使用';
            $item['bind_status'] = YunPhoneUserMachine::bindStatus[$item['bindStatus']] ?? '未绑定';

        }
        //获取数据总数
        $count = $this->model->where($where)->count();

        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $data,
            "count" => $count,
        ];
    }

    /**
     * Notes: 退出挂机
     * User: liubaoqiang
     * DateTime: 2024/12/13 15:55
     * @return array
     */
    public function closeUserMachine(): array
    {
        $params = request()->param();
        if (empty($params['member_id'] || empty($params['user_device_id']))) {
            return message("缺少参数", false);
        }
        $result = YunPhoneService::stopYunPhoneUser($params['member_id'], $params['user_device_id']);
        if ($result['code'] != 1) return message($result['message'], false);
        return message('退出挂机成功!');
    }

    /**
     * Notes: 修改到期时间
     * User: liubaoqiang
     * DateTime: 2024/12/13 15:59
     * @return array
     */
    public function editUserMachine(): array
    {
        $params = request()->param();
        if (empty($params['member_id'] || empty($params['user_phone_id']) || empty($params['valid_end_time']))) {
            return message("缺少参数", false);
        }
        $param = [
            'userDeviceId' => $params['user_device_id'],
            'validEndTime'   => $params['valid_end_time'],
        ];
        $result = YunPhoneService::updateYunPhoneUserValidate($params['member_id'], $param);
        if ($result['code'] != 1) return message($result['message'], false);
        return message('修改到期时间成功!', true, $result['data'] ?? '');
    }

    /**
     * Notes: 回收设备
     * User: liubaoqiang
     * DateTime: 2024/12/13 16:04
     * @return array
     */
    public function recycleUserMachine(): array
    {
        $params = request()->param();
        if (empty($params['member_id'] || empty($params['user_device_id']))) {
            return message("缺少参数", false);
        }
        $result = YunPhoneService::recoveryYunPhoneUser($params['member_id'], $params['user_device_id']);
        if ($result['code'] != 1) return message($result['message'], false);
        return message('回收设备成功!', true, $result['data']);
    }
}
